'use strict';
app.controller('printerCtrl', ['$scope','$state','toaster','$modal', '$log','utilService','printerService', function($scope,$state,toaster,$modal,$log,utilService,printerService) {

    //查询打印机列表
    $scope.findPageAll = function () {
        utilService.queryForPagingList($scope, {
            url: $scope.app.domain + 'printer'
        }).then(function (result) {
        });
    }

    $scope.findPageAll();

    //打开添加窗口
    $scope.openAddPoup=function(){
        var modalInstance = $modal.open({
            templateUrl: 'tpl/printer/printer_add.html',
            controller: 'printerAddCtrl',
            size: 'md',
            scope:$scope,
            backdrop:'static'
        });

        modalInstance.result.then(function (result) {
            $scope.findPageAll();
        }, function () {
            $log.info('Modal dismissed at: ' + new Date());
        });
    }

    //打开添加窗口
    $scope.openEditPoup=function(id){
        var modalInstance = $modal.open({
            templateUrl: 'tpl/printer/printer_edit.html',
            controller: 'printerEditCtrl',
            size: 'md',
            scope:$scope,
            backdrop:'static',
            resolve:{
                data:function(){
                    return {id:id};
                }
            }
        });

        modalInstance.result.then(function (result) {
            $scope.findPageAll();
        }, function () {
            $log.info('Modal dismissed at: ' + new Date());
        });
    }

    //打开设置窗口
    $scope.openSetPoup=function(id){
        var modalInstance = $modal.open({
            templateUrl: 'tpl/printer/printer_set.html',
            controller: 'printerSetCtrl',
            size: 'sm',
            scope:$scope,
            resolve:{
                data:function(){
                    return {id:id};
                }
            }
        });

        modalInstance.result.then(function (result) {
            $scope.findPageAll();
        }, function () {
            $log.info('Modal dismissed at: ' + new Date());
        });
    }



    //查询小票模板信息
    $scope.del = function(id,name){
        var param = {
            id:id,
            name:name
        };

        openModal("您确定要["+name+"]删除该打印机?");

        function openModal(msg){
            utilService.modal($modal, $scope, {
                title:"删除打印机",
                controller:"PrinterDelCtrl",
                message:msg,
                data:param
            });
        }
    }


}]);

app.controller('printerAddCtrl', ['$scope','$modalInstance','$state','toaster','printerService', function($scope, $modalInstance,$state,toaster,printerService) {
    $scope.printer={number:1,paperWidthType:0,printerType:0,type:3};

    //查询小票模板信息
    $scope.queryPrinterTemplate=function(){
        printerService.queryPrinterTemplate($scope).then(function (result) {
            switch (result.status) {
                case 200:
                    $scope.printerTemplate=result.data;
                    break;
                default :
                    break;
            }
        })
    }

    $scope.queryPrinterTemplate();

    //添加打印机
    $scope.add=function(){
        printerService.add($scope).then(function (result) {
            switch (result.status) {
                case 200:
                    $modalInstance.close("success");
                    break;
                case 726:
                    toaster.pop('waiting', "提示", result.message);
                    break;
                case 16:
                case 35:
                    toaster.pop('waiting', "提示", result.message);
                    break;
                default :
                    break;
            }
        })
    }

    //查询打印机子小票模板
    $scope.print=function(){
        printerService.print($scope).then(function (result) {
            switch (result.status) {
                case 200:
                    break;
                case 16:
                case 35:
                    //toaster.pop('waiting', "提示", result.message);
                    toaster.pop('waiting', "提示", "打印机信息错误");
                default :
                    break;
            }
        })
    }

    $scope.cancel = function () {
        $modalInstance.dismiss("关闭");
    };

}]);

app.controller('printerEditCtrl', ['$scope','$modalInstance','$state','toaster','printerService','data', function($scope, $modalInstance,$state,toaster,printerService,data) {
    $scope.id=data.id;
    $scope.printer={id:data.id};

    //查询小票模板信息
    $scope.queryPrinterTemplate=function(){
        printerService.queryPrinterTemplate($scope).then(function (result) {
            switch (result.status) {
                case 200:
                    $scope.printerTemplate=result.data;
                    break;
                default :
                    break;
            }
        })
    }

    $scope.queryPrinterTemplate();


    //查询打印机子小票模板
    $scope.findById=function(){
        printerService.findById($scope).then(function (result) {
            switch (result.status) {
                case 200:
                    $scope.printer=result.data;
                    break;
                default :
                    break;
            }
        })
    }

    $scope.findById();


    //查询打印机子小票模板
    $scope.print=function(){
        printerService.print($scope).then(function (result) {
            switch (result.status) {
                case 200:
                    break;
                case 16:
                case 35:
                    //toaster.pop('waiting', "提示", result.message);
                    toaster.pop('waiting', "提示", "打印机信息错误");
                    break;
                default :
                    break;
            }
        })
    }

    //添加打印机
    $scope.edit=function(){
        printerService.edit($scope).then(function (result) {
            switch (result.status) {
                case 200:
                    $modalInstance.close("success");
                    break;
                case 726:
                    toaster.pop('waiting', "提示", result.message);
                    break;
                case 16:
                case 35:
                    $modalInstance.close("success");
                    toaster.pop('waiting', "提示", result.message);
                    break;
                default :
                    break;
            }
        })
    }



    $scope.cancel = function () {
        $modalInstance.dismiss("关闭");
    };

}]);


app.controller('PrinterDelCtrl', ['$scope', '$modalInstance','$state','printerService','toaster','modalData',
    function($scope, $modalInstance,$state,printerService,toaster,modalData) {
        $scope.modal = modalData;
        $scope.id=$scope.modal.data.id;

        $scope.isDelModal=true;

        //确认删除
        $scope.ok = function(){
            if(!$scope.isDelModal){
                return;
            }
            $scope.isDelModal = false;
            printerService.del($scope).then(function(result){
                switch (result.status){
                    case 200:
                        $modalInstance.close('success');
                        toaster.pop('success', "提示", "操作成功");;
                        $scope.isDelModal=true;
                        $scope.findPageAll();
                        break;
                    case 726:
                        toaster.pop('waiting', "提示", result.message);
                        $scope.isDelModal=true;
                        break;
                    default :
                        toaster.pop('error', "操作失败", "发生未知错误！请联系管理员");
                        break;
                }
            });
        }
        //取消弹窗
        $scope.cancel = function () {
            $modalInstance.dismiss('cancel');
        };
    }
]);


app.controller('printerSetCtrl', ['$scope','$modalInstance','$state','toaster','printerService','data', function($scope, $modalInstance,$state,toaster,printerService,data) {
    $scope.id=data.id;

    $scope.printerVoiceVO={
        soundType:0,
        sound:0,
        isOpenVoice:0,
        voice:1
    };


    //查询打印机子小票模板
    $scope.findById=function(){
        printerService.findById($scope).then(function (result) {
            switch (result.status) {
                case 200:
                    var data=result.data;
                    $scope.printerVoiceVO.soundType=data.soundType;
                    $scope.printerVoiceVO.sound=data.sound;
                    $scope.printerVoiceVO.isOpenVoice=data.isOpenVoice;
                    $scope.printerVoiceVO.voice=data.voice;

                    break;
                default :
                    break;
            }
        })
    }

    $scope.findById();

    //关机
    $scope.shutdown=function(){
        printerService.shutdown($scope).then(function (result) {
            switch (result.status) {
                case 200:
                    $scope.findPageAll();
                    break;
                default :
                    break;
            }
        })
    }

    //重启
    $scope.restart=function(){
        printerService.restart($scope).then(function (result) {
            switch (result.status) {
                case 200:
                    $scope.findPageAll();
                    break;
                default :
                    break;
            }
        })
    }

    //设置打印机声音
    $scope.setSound=function(){
        printerService.setSound($scope).then(function (result) {
            switch (result.status) {
                case 200:
                    break;
                default :
                    break;
            }
        })
    }

    //自定义打印机声音文字
    $scope.setVoice=function(){
        printerService.setVoice($scope).then(function (result) {
            switch (result.status) {
                case 200:
                    break;
                default :
                    break;
            }
        })
    }

    $scope.cancel = function () {
        $modalInstance.dismiss("关闭");
    };

}]);
